package com.my.style.ui.activity;

import android.os.Bundle;
import android.view.View;

import com.my.style.basemodul.HttpApi;
import com.sv.jm.baselibrary.Coustom.HttpObserver;
import com.sv.jm.baselibrary.Coustom.NetWorkException;
import com.sv.jm.baselibrary.base.BaseActivity;
import com.sv.jm.baselibrary.event.EventAction;
import com.sv.jm.baselibrary.response.BaseRespone;
import com.sv.jm.baselibrary.utils.LogHelper;

import java.util.HashMap;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class HttpPostDemo extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getContainerLayout() {
        return 0;
    }

    @Override
    public void OnCoustomLayoutCreated(View v) {
        getServerData();
    }

    private void getServerData() {
        Map<String, String> header = new HashMap<>();//设置请求头信息
        header.put("user_mobile","15683155386");
        header.put("user_code","12345");
        try {
            showLoading(null);
            HttpApi.getInstance(this).setHeader(header)
                    .loginService("39811c585b1932f5b7f68c52dab6ab1bdb85c4d25df75f61b58df929b124ed3a24b2eba4cb10f0181a484b2ab500fb596b765fe8745f273e2e43097924f313e8800c5bc1167c7de4a1b0feb62efc5810138ef0f2145ebb5402051e0cbdc0409a56f1bad72d46a20f2b19a5d3de11dea6c64bfa4d9e2bec87ba0a156de8b13e231038bfe52fa4bc3342a665a58072898db6aa8ce0aaa6fcdb7260b74b35949a7fbbc9422cdb1f949dd2c3df163a941a15f965c5c6fd9f20f9")
                    .subscribeOn(Schedulers.newThread())//调用切换之前的线程
                    .observeOn(AndroidSchedulers.mainThread())//调用切换之后的线程(观察者在主线程执行任务)
                    // 注：调用切换之后的线程方法后在调用切换之前的线程方法（无效）
                    .subscribe(new HttpObserver(this) {
                        @Override
                        public void onResult(BaseRespone baseRespone) {
                            LogHelper.logE(HttpPostDemo.class,baseRespone.toString());
                        }
                    });
        } catch (NetWorkException e) {//检测没有网络的时候会抛出此异常  自己做处理
            hideLoading();
            showError(e.getMessage());
        }
    }

    @Override
    protected void onErrorClick() {
        hideError();
        getServerData();
    }

    @Override
    public void leftClick() {

    }

    @Override
    public void rightClick() {

    }

    @Override
    public void otherClick(View v) {

    }

    @Override
    protected void onRefreshData() {

    }

    @Override
    public void onOtherActionReceiver(EventAction eventAction) {

    }

}
